﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace DalatCooking.Core
{
    public class CartAccessor
    {
        List<Cart> list;
        public List<Cart> ShoppingCart { get { return list; } }
        public CartAccessor()
        {
            list = new List<Cart>();
        }
        public int SearchInCart(int itemId)
        {
            int index = 0;
            foreach (var item in list)
            {
                if (list[index].Compare(itemId))
              
                    return index;
                
                index++;
            }
            return -1;
        }
        public void AddToCart(Cart entity)
        {
            int index = SearchInCart(entity.CourseId);
            if (index > 0)
            {
                list[index].Quantity++;
            }
            else list.Add(entity);
        }
        public void RemoveCart(int index)
        {
            if (index > 0 && index < list.Count)
                list.RemoveAt(index);
        }
        public void UpdateCart(int index, int quantity)
        {
            if (index > 0 && index < list.Count)
                list[index].Quantity = quantity;
        }
        public float Totals()
        {
            float total = 0;
            for (int i = 0; i < list.Count; i++)
                total += list[i].SubTotal;
            return total;
        }
        public int CountQuantity()
        {
            int quantity = 0;
            for (int i = 0; i < list.Count; i++)
                quantity = list[i].Quantity;
            return quantity;
        }
    }
}
